using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using FrameOperateSync;


public class KeyboardController : MonoBehaviour, IInputController
{
    [Header("Move keys")]
    public KeyCode upKey;
    public KeyCode leftKey;
    public KeyCode downKey;
    public KeyCode rightKey;

    [Space(5)]
    public KeyCode[] actionKeys;

    private Vector2 moveVelocity = Vector2.zero;

    public event Action StopEvent;
    public event Action<int> MoveEvent;
    public event Action<byte> ButtonEvent;

    void Start()
    {
        //base.Start();
    }

    void Update()
    {
        Vector2 oldVelocity = moveVelocity;

        moveVelocity = Vector2.zero;
        if (Input.GetKey(rightKey))
        {
            moveVelocity.x += 1;
        }
        if (Input.GetKey(leftKey))
        {
            moveVelocity.x -= 1;
        }
        if (Input.GetKey(upKey))
        {
            moveVelocity.y += 1;
        }
        if (Input.GetKey(downKey))
        {
            moveVelocity.y -= 1;
        }

        if (oldVelocity != moveVelocity)
        {
            if (moveVelocity.sqrMagnitude <= Mathf.Epsilon)
            {
                //OnStopMove();
                StopEvent?.Invoke();
            }
            else
            {
                //OnStartMove((int)(Mathf.Atan2(moveVelocity.y, moveVelocity.x) * Mathf.Rad2Deg));
                MoveEvent?.Invoke((int)(Mathf.Atan2(moveVelocity.y, moveVelocity.x) * Mathf.Rad2Deg));
            }
        }


        if (actionKeys != null)
        {
            for (int i = 0; i < actionKeys.Length; i++)
            {
                if (Input.GetKeyDown(actionKeys[i]))
                {
                    //OnActiveSlot((byte)i);
                    ButtonEvent?.Invoke((byte)i);
                }
            }
        }
    }
}
